package it.polimi.traveldream.ejb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import it.polimi.traveldream.ejb.management.EscursioneDTO;
import it.polimi.traveldream.ejb.management.HotelDTO;
import it.polimi.traveldream.ejb.management.PPrenotatiDTO;
import it.polimi.traveldream.ejb.management.PackDTO;
import it.polimi.traveldream.ejb.management.PrenotazMng;
import it.polimi.traveldream.ejb.management.VoloDTO;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class BeanPrenotazione implements PrenotazMng{
	@PersistenceContext
    private EntityManager em;
	
	@Resource
	private EJBContext context;

	
	
	@Override
	public List<HotelDTO> hotelFiltrati(String filtro){
		Query query=em.createNamedQuery(Hotel.FILTR);
		query.setParameter(1,filtro);
		List<Hotel> lists=castList(Hotel.class,query.getResultList());
        List<HotelDTO> list=convertToDTO(lists);
		return list;
	}
	
	private List<HotelDTO> convertToDTO (List<Hotel> hotDaConv) {
		List<HotelDTO> returnedItems=new ArrayList<HotelDTO>();
		Iterator<Hotel> iter=hotDaConv.iterator();
		while(iter.hasNext()){
			returnedItems.add(convertToDTO(iter.next()));
		}
		return returnedItems;
	}
	
	public HotelDTO convertToDTO(Hotel hotel){
		HotelDTO hotelDTO = new HotelDTO();
		hotelDTO.setNome(hotel.getNome());
		hotelDTO.setCitta(hotel.getCitta());
		hotelDTO.setPostiRimanenti(hotel.getPostiRimanenti());
		hotelDTO.setPrezzoSingolo(hotel.getPrezzoSingolo());
		hotelDTO.setIdHotel(hotel.getIdHotel());
		return hotelDTO;
		
	}

	public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
	    List<T> r = new ArrayList<T>(c.size());
	    for(Object o: c)
	      r.add(clazz.cast(o));
	    return r;
	}
	
	@Override
	public List<VoloDTO> voliAndFiltrati(String filtro){
		Query query=em.createNamedQuery(Volo.FILTERAND);
		query.setParameter(1,filtro);
		List<Volo> lists=castList(Volo.class,query.getResultList());
        List<VoloDTO> list=convertToVoloDTO(lists);
		return list;
	}
	
	@Override
	public List<VoloDTO> voliRitFiltrati(String filtro){
		Query query=em.createNamedQuery(Volo.FILTERRIT);
		query.setParameter(1,filtro);
		List<Volo> lists=castList(Volo.class,query.getResultList());
        List<VoloDTO> list=convertToVoloDTO(lists);
		return list;
	}
	
	public VoloDTO convertToDTO(Volo volo) {
		VoloDTO voloDTO = new VoloDTO();
		voloDTO.setIdVolo(volo.getIdVolo());
		voloDTO.setNumVolo(volo.getNumVolo());
		voloDTO.setPostiRimanenti(volo.getPostiRimanenti());
		voloDTO.setPrezzo(volo.getPrezzo());
		voloDTO.setAeroportoArrivo(volo.getAeroportoArrivo().getCodIata());
		voloDTO.setAeroportoPartenza(volo.getAeroportoPartenza().getCodIata());
		voloDTO.setCompagnia(volo.getCompagnia());
		voloDTO.setData(volo.getData());
		return voloDTO;
	}
	
	private List<VoloDTO> convertToVoloDTO (List<Volo> voloDaConv) {
		List<VoloDTO> returnedItems=new ArrayList<VoloDTO>();
		Iterator<Volo> iter=voloDaConv.iterator();
		while(iter.hasNext()){
			returnedItems.add(convertToDTO(iter.next()));
		}
		return returnedItems;
	}
	
	@Override
	public void execQuery(PackDTO pack){
		Query query=em.createNamedQuery(Pacchetti.UPDDISP);
		query.setParameter(1,pack.getId());
		query.executeUpdate();
		Query query2=em.createNamedQuery(Escursione.UPDDISP);
		query2.setParameter(1,pack.getEscursione());
		query2.executeUpdate();
		Query query3=em.createNamedQuery(Volo.UPDDISP);
		query3.setParameter(1,pack.getVoloAndata());
		query3.executeUpdate();
		Query query4=em.createNamedQuery(Volo.UPDDISP);
		query4.setParameter(1,pack.getVoloRitorno());
		query4.executeUpdate();
		Query query5=em.createNamedQuery(Hotel.UPDDISP);
		query5.setParameter(1,pack.getHotel());//---------
		query5.executeUpdate();
	}
	
	@Override
	public void save(PPrenotatiDTO pacchetto){
		PacchettiPrenotati pack=new PacchettiPrenotati();
		pack.setIdescursione(pacchetto.getIdescursione());
		pack.setIdhotel(pacchetto.getIdhotel());
		pack.setIdvoloandata(pacchetto.getIdvoloandata());
		pack.setIdvoloritorno(pacchetto.getIdvoloritorno());
		pack.setDataPartenza(pacchetto.getDataPartenza());
		pack.setDataRitorno(pacchetto.getDataRitorno());
		pack.setLuogo(pacchetto.getLuogo());
		pack.setNomePacchetto(pacchetto.getNomePacchetto());
		Query query = em.createNamedQuery(Utente.FIND_MAIL,Utente.class).setParameter(1, pacchetto.getMailutente());
		pack.setUtente((Utente)query.getSingleResult());
		em.persist(pack);
	}

	@Override
	public List<EscursioneDTO> getEscursioniCitta(String citta) {
		Query query=em.createNamedQuery(Escursione.FILTR);
		
		query.setParameter(1,citta);
		List<Escursione> lists=castList(Escursione.class,query.getResultList());
		List<EscursioneDTO> list = new ArrayList<EscursioneDTO>();
		for (Escursione e:lists) {
			list.add(convertToDTO(e));
		}
		return list;
	}
	
	private EscursioneDTO convertToDTO(Escursione escursioneBean) {
		EscursioneDTO escursione = new EscursioneDTO();
		escursione.setCitta(escursioneBean.getCittaBean());
		escursione.setIdEscursione(escursioneBean.getIdEscursione());
		escursione.setNome(escursioneBean.getNome());
		escursione.setInizio(escursioneBean.getData());
		escursione.setPostiRimanenti(escursioneBean.getPostiRimanenti());
		escursione.setPrezzoSingolo(escursioneBean.getPostiRimanenti());
		return escursione;
	}
	
}

